package com.jhws.sys.appUpgrade.controller;

import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.core.exception.BizException;
import com.jhws.sys.appUpgrade.bo.AppUpdateQR;
import com.jhws.sys.appUpgrade.service.AppUpdateQRService;
import com.jhws.util.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/appupdateqr")
public class AppUpdateQRContorller {

    @Resource
    private AppUpdateQRService appUpdateQRService;
    @Resource
    private OSSClientService ossClientService;


    @RequestMapping("saveAppUpdateQR.x")
    @ResponseBody
    public JsonRespData saveAppUpdateQR(AppUpdateQR appUpdateQR) throws Exception {
        LogHelper.d("-------------->    " + appUpdateQR.toString());
        //将临时文件拷贝
        if (StringUtils.isNotEmpty(appUpdateQR.getAppIoc())) {
            String suffix = FilenameUtils.getExtension(appUpdateQR.getAppIoc());
            String imagePath = "cms/file/" + GetId.randomID() + "." + suffix;
            ossClientService.gerneralCopyFile(appUpdateQR.getAppIoc(), imagePath);
            appUpdateQR.setAppIoc(imagePath);
        }

        appUpdateQRService.saveAppUpdateQR(appUpdateQR);
        return new JsonRespData("", 1, "");
    }

    @RequestMapping("queryAppUpdateQR.x")
    @ResponseBody
    public JsonRespData queryAppUpdateQR(String id) {
        AppUpdateQR appUpdateQR = appUpdateQRService.queryAppUpdateQR(id);
        return new JsonRespData("", 1, appUpdateQR);
    }

    @RequestMapping("queryAppUpdateQRList.x")
    @ResponseBody
    public JsonRespData queryAppUpdateQRList() {
        Map appUpdateQRS = appUpdateQRService.queryAppUpdateQRByWhere("");
        List<AppUpdateQR> appUpdateQRList = (List<AppUpdateQR>) appUpdateQRS.get("list");
        if (appUpdateQRList != null && appUpdateQRS.size() > 0) {
            for (AppUpdateQR appUpdateQR : appUpdateQRList) {
                appUpdateQR.setAppIocUrl(ossClientService.getUrl(appUpdateQR.getAppIoc(), false));
            }
        }
        return new JsonRespData("", 1, appUpdateQRS);
    }

    @RequestMapping("deleteAppUpdateQR.x")
    @ResponseBody
    public JsonRespData deleteAppUpdateQR(AppUpdateQR appUpdateQR) {
        appUpdateQRService.deleteAppUpdateQR(appUpdateQR);
        return new JsonRespData("", 1, "");
    }

    @RequestMapping("updateAppIoc.x")
    @ResponseBody
    public JsonRespData redistributionOutdoorAdv(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        String suffix = FilenameUtils.getExtension(file.getOriginalFilename());
        String fileName = GetId.randomID();
        String path = "temp_1day/" + fileName + "." + suffix;
        ossClientService.updateFile(path, file.getInputStream());
        String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                + request.getContextPath();
        url += "/cms/resource/downloadRemoteFile.x?path=" + path;
        Map<String, String> ret = new HashMap<>();
        ret.put("appIoc", path);
        ret.put("appIocUrl", url);
        return new JsonRespData("", 1, ret);
    }


    @RequestMapping("qrcode.x")
    public void createQRCode(HttpServletResponse response, HttpServletRequest request, String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new BizException("-1", "", "参数错误！");
        }
        String domainName = request.getScheme() + "://" + request.getServerName();
        //禁止缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Content-Disposition", "attachment;fileName=qrcode.jpg");
        String webUrl = "/pages/mobile/appupdate/bundle/index.html?id=" + id;
        String url = domainName + webUrl;
        ImageIO.write(QRCodeUtil.createImage(url, null, true), "JPEG", response.getOutputStream());
        response.getOutputStream().flush();
    }
}
